import { ExtendedRoute } from '@/stores'
import { RouteLocationNormalized } from 'vue-router'

export function beforeEach(_to: RouteLocationNormalized, from: RouteLocationNormalized) {
	const adminMain = document.querySelector('#adminui-main') as HTMLElement
	if (!adminMain) return false
	const viewTag = useViewTagStore()
	viewTag.updateViewTags({ fullPath: from.fullPath, scrollTop: adminMain.scrollTop } as ExtendedRoute)
}

export function afterEach(to: RouteLocationNormalized, _from: RouteLocationNormalized) {
	const adminMain = document.querySelector('#adminui-main') as HTMLElement
	if (!adminMain) return false
	const viewTag = useViewTagStore()
	nextTick(() => {
		const beforeRoute = viewTag.viewTags.find((v: ExtendedRoute) => v.fullPath === to.fullPath)
		if (beforeRoute) {
			adminMain.scrollTop = beforeRoute.scrollTop || 0
		}
	})
}
